﻿define(["jquery","bbcare","jqprint",
    "scprit/department/vm/assessmentScreening/nbnaAssessment.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"
],function ($,$bb,$jqprint,$ui,$servers) {
    var event = {};
    event.init = function(obj){
        var userId = obj.userId,type = obj.type,answerId = obj.answerId;
        var hisParams = $servers.hisParams(userId,type,'1','');
        var hisFn = function(data){
            for (var i = 0; i < data.rows.length; i++) {
                var testTime = data.rows[i].createTime.substring(0,10);
                data.rows[i].testAge = $servers.age($servers.dateDiff(data.rows[i].birthday,testTime),data.rows[i].pregnancyWeek);
                data.rows[i].testTime = testTime;
            }
            $ui.historyInit({"id":"#nbna-history","data":data.rows});
            if(!answerId){
                answerId = $("#nbna-history>li").eq(0).attr("answerId");
            }
            checkedFn(answerId);
            getInfo(userId,answerId);
            getGuides(userId,type,answerId);
            //历史记录列表点击事件：
            $("#nbna-history>li").click(function(){
                $(this).addClass("select").siblings().removeClass("select");
                answerId = $(this).attr("answerId");
                getInfo(userId,answerId);
                getGuides(userId,type,answerId);
            });
            /******************** begin 南方医院 添加备注 ********************/
            $("#modify").click(function(){
                if($(this).attr("type")=="modify"){
                    $(".guides").attr("contentEditable",true).addClass("edit");
                    if($("#guides").html() == "暂无指导意见"){
                        $("#guides").html("");
                    }
                    $(this).attr("type","save").html('<i class="fa fa-check-square" aria-hidden="true"></i>&nbsp;确认修改');
                }else if($(this).attr("type")=="save"){
                    $(".guides").removeAttr("contentEditable").removeClass("edit");
                    $(this).attr("type","modify").html('<i class="fa fa-pencil-square" aria-hidden="true"></i>&nbsp;修改建议');
                    var guides = $.trim($("#guides").html());
                    var remark = $( "#remark").length > 0 ? $.trim($( "#remark").html()) : "";
                    if(!guides){
                        $("#guides").html("暂无指导意见");
                        guides = "暂无指导意见";
                    }
                    modifyGuides(userId,answerId,guides,remark,type);
                }
            });
            $("#modifyRemark").click(function(){
                if($(this).attr("type")=="modify"){
                    $(".remark").attr("contentEditable",true).addClass("edit");
                    if($("#remark").html() == "暂无备注"){
                        $("#remark").html("");
                    }
                    $(this).attr("type","save").html('<i class="fa fa-check-square" aria-hidden="true"></i>确认备注');
                }else if($(this).attr("type")=="save"){
                    $(".remark").removeAttr("contentEditable").removeClass("edit");
                    $(this).attr("type","modify").html('<i class="fa fa-pencil-square" aria-hidden="true"></i>修改备注');
                    var remark = $.trim($("#remark").html());
                    var guides = $.trim($("#guides").html());
                    if(!remark){
                        $("#remark").html("暂无备注");
                        remark = "暂无备注";
                    }
                    modifyGuides(userId,answerId,guides,remark,type);
                }
            });
            /******************** end 南方医院 ********************/
        };
        $servers.history(hisParams,hisFn);
    };
    //获取评估记录详情：
    var getInfo = function(userId,evaId){
        var reportParams = $servers.reportParams(userId,evaId);
        var reportFn = function(data){
            var testTime = data.data.createTime.substring(0,10);

			$("#testTime,#testTimePrint").html(testTime);
			
			var hostitle = data.data.deptName;
            $("#hos-title,#hos-titlePrint,#testHos").html(hostitle);
			
			var phoneNo = data.data.deptPhone; //评估科电话
			if(phoneNo!=null & phoneNo!=""){
				var testTel=$("#testTel_hide").html();
				if(testTel==undefined){
					$("#footer ul li:last").append('<li id="testTel_hide">评估科电话： <span id="testTel"></span></li>');
					$("#testTel").html(phoneNo);
					$(".footerPrint ul li:last").append('<li id="testTelPrint_hide">评估科电话： <span id="testTel_Print"></span></li>');
					$("#testTel_Print").html(phoneNo);
				}
			}
			
            data.data.testAge = $servers.age($servers.dateDiff(data.data.birthday,testTime),data.data.pregnancyWeek);
            $ui.resultInfoInit({"id":"#basic-info","data":data.data});
            $ui.resultInfoInit({"id":"#print-basicInfo","data":data.data});
            var width = $("#zone-score").width();
            var othersWidth = width - 62*5;
            var result = JSON.parse(data.data.result);
            result.totalScore = 0;
            for(var item in result){
                if(item != "totalScore"){
                    if(item == "ability"){
                        result["behaviorAbility"] = result[item];
                        item = "behaviorAbility";
                        delete result['ability'];
                    }
                    result.totalScore += result[item]*1;
                }
            }
            for(var its in result){
                if(its != "totalScore"){
                    if(result.totalScore*1 > 0){
                        result[its+"Width"] = 100*(othersWidth*result[its]*1/result.totalScore + 62)/width + "%";
                    }else{
                        result[its+"Width"] = "20%";
                    }
                }
            }
            result.level = result.totalScore*1 > 35 ? "合格":"不合格";
            $ui.resultInit({"id":"#zone-score","data":result});
            $ui.resultInit({"id":"#print-zoneScore","data":result});
        };
        $servers.report(reportParams,reportFn);
    };
    //获取指导意见
    var getGuides = function (userId,exam,evaId){
        var params = $servers.getGuidesParams(userId,exam,evaId);
        var guidesFn = function(data){
            if( data.data ){
                $("#guides,#print-guides").html(data.data.guide);
                if( $("#remark").length > 0 ) $("#remark").html(data.data.remark)
            }else{
                $("#guides,#print-guides").html("暂无指导意见");
                if( $("#remark").length > 0 ) $("#remark").html("暂无备注")
            }
        };
        $servers.getGuides(params,guidesFn);
    };
    //修改建议：
    var modifyGuides = function(userId,evaId,guides,remark,exam){
        var modifyPramas = $servers.modifyGuidesParams(userId,evaId,guides,remark);
        var modifyFn = function(data){
            getGuides(userId,exam,evaId);
        };
        $servers.modifyGuides (modifyPramas,modifyFn);
    };
    //加载样式：
    var checkedFn = function(answerId){
        $("#nbna-history").find("li").each(function(i){
            var $this=$(this);
            if($this.attr("answerId")==answerId){
                $(this).addClass("select").siblings().removeClass("select");
            }
        });
    };
    return event;
});